//保存表格的实例对象
var tableI = null;

function aa(){
    tableI.reload();
}

layui.use(['form','laydate','upload'], function () {
    var form = layui.form;
    var laydate = layui.laydate;
    var upload = layui.upload;

    // 修改时间
    laydate.render({
        elem: '#hirthdate',
        type: 'datetime'
    })

    // 点击展示ztree树表
    $("#ztree").on('click',function () {
        var index = layer.open({
            type: 2,
            title: '',
            content: 'productztree.html',
            area: ['80%', '80%'],
            end:function (){
                var ww = localStorage.getItem("treeid");
                $("#treeid").val(ww);
                var ww1= localStorage.getItem("treename");
                $("#aa").val(ww1);
            }
        })
    })

    // 点击取消，关闭页面
    $("#cancel").click(function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    })

    // 修改
    form.on('submit(determine)',function (obj){
        $.ajax({
            type:'post',
            url:'/edit/update',
            data:obj.field,
            dataType:'json',
            success:function (res){
                if (res.state == 200){
                    //先得到当前iframe层的索引
                    layer.msg("修改用户信息成功",{time: 1500,icon:6},function (){
                        let index = parent.layer.getFrameIndex(window.name);
                        //再执行关闭
                        parent.layer.close(index);
                        //刷新主页面
                        //调用自己写的重载方法
                        parent.aa();
                    })
                }else {
                    layer.msg("修改用户信息失败",{time: 1500,icon:5},function (){
                        let index = parent.layer.getFrameIndex(window.name);
                        //再执行关闭
                        parent.layer.close(index);
                    });
                }
            },
            error:function (e){
                console.log("失败");
                console.log(e);
            }
        })
    })

    //图片上传
    var uploadInst = upload.render({
        elem: '#upload'
        ,url: '/YltUpload'
        ,accept:'images'
        // 多图片上传
        ,multiple: true
        //限定大小
        ,size: 1024*5
        ,acceptMime:'image/jpg|image/png'
        ,before: function(obj){
            obj.preview(function(index, file, result){
                $("#commodityImage").attr('src', result);
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code > 0){
                return layer.msg('上传失败');
            }
            //上传成功
            else {
                console.log(res.data.fullPath);
                $("input[name='commodityImage']").val(res.data.fullPath);
                return layer.msg('上传成功');
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            layer.msg('失败');
        }
    })
})